home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2FG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  7.2 KB  |  262 lines

  1. #include <fastgraf.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7.  
  8. /*
  9. ╔═════════════════════════════════════════════════════════════════╗
  10. ║             █▓▒░ WordUp Graphics Toolkit V4.0 ░▒▓█              ║
  11. ║ Conversion Utility   Copyright 1994 WordUp Software Productions ║
  12. ╟─────────────────────────────────────────────────────────────────╢
  13. ║ Program:      spr2fg.c                                          ║
  14. ║ Description:  Utility for converting a WGT sprite file to       ║
  15. ║               a Fastgraph format.                               ║
  16. ║                                                                 ║
  17. ║ Written by:   Chris Egerter                                     ║
  18. ╚═════════════════════════════════════════════════════════════════╝
  19. */
  20.  
  21.  
  22. /*
  23. ╔═════════════════════════════════════════════════════════════════╗
  24. ║  WGT Sprite File to Fastgraph conversion program                ║
  25. ║                                                                 ║
  26. ║  The file format is not one already used in Fastgraph, but      ║
  27. ║  an altered version of the WGT sprite format.                   ║
  28. ║                                                                 ║
  29. ║  Note that this version works only in 256 color modes.          ║
  30. ╚═════════════════════════════════════════════════════════════════╝
  31. */
  32.  
  33.  
  34.  
  35. unsigned char *sprites[2001];
  36. void convertsprites (char *infile, char *outfile);
  37. void loadsprites (char *infile, void far *loadspr[]);
  38. void testsprites ();
  39. void freesprites (void far *freespr[]);
  40.  
  41. int oldmode;
  42. int width[2001], height[2001];
  43.  
  44.  
  45. int main(void)
  46. {
  47.  oldmode = fg_getmode ();
  48.  fg_setmode (19);  /* This mode can be any 256 color graphics mode */
  49.  
  50.  convertsprites ("sprtconv.spr", "out.spr");
  51.  loadsprites ("out.spr", sprites);
  52.  testsprites ();
  53.  freesprites (sprites);
  54.  
  55.  
  56.  /* clean up */
  57.  fg_setmode (oldmode);
  58.  return 0;
  59. }
  60.  
  61. void convertsprites(char *infile, char *outfile)
  62. {
  63. FILE *in;   /* 256 color sprite file */
  64. FILE *out;  /* converted sprite file */
  65. unsigned char palette[768];         /* 256 * 3 (RGB values) */
  66. int maxcolor;
  67. int maxsprite;
  68. unsigned size;
  69.  
  70. char far *temp;
  71. int a, b, i, spritemade;
  72. char buf[14];
  73. unsigned char c;
  74. int x, y;
  75. int startingsprite;
  76.  
  77.  /* Open the files */
  78.  if ((in = fopen (infile, "rb")) == NULL)
  79.   {
  80.    fg_setmode (oldmode);
  81.    printf ("Could not open 256 color sprite file");
  82.    exit (1);
  83.   }
  84.  if ((out = fopen (outfile, "wb")) == NULL)
  85.   {
  86.    fg_setmode (oldmode);
  87.    printf ("Could not open converted sprite file");
  88.    exit (1);
  89.   }
  90.  
  91.  fread(&a, 1, 2, in);
  92.      /* Get the version number, and change the startingsprite accordingly.
  93.      If version <= 3, maxsprite contains the maximum number of sprites
  94.      that can be stored in a file.  If version > 4, maxsprite contains
  95.      the number of the highest sprite in the file. (empty sprites at
  96.      the end are not kept in the file. */
  97.  if (a <= 3)
  98.    startingsprite = 1;
  99.  else startingsprite = 0;    /* Version 4 starts at sprite 0 */
  100.  
  101.  
  102.  
  103.  fread (buf, 1, 13, in); /* sprite header */
  104.  if (0 == strnicmp (" Sprite File ", buf, 13)) 
  105.   /* see if it is a sprite file */
  106.   {
  107.    fread (palette, 1, 768, in); /* Read in 256 color palette */
  108.    maxcolor = 256;
  109.    fwrite(&maxcolor, 1, 2, out); 
  110.    /* Write the number of colors stored in file */
  111.  
  112.    for (i = 0; i < maxcolor; i++) /* read in the palette */
  113.     {
  114.      fputc (palette[i*3],   out);    /* Write out Red */
  115.      fputc (palette[i*3+1], out);    /* Green */
  116.      fputc (palette[i*3+2], out);    /* And Blue color values */
  117.     }
  118.         
  119.    fread(&maxsprite, 1, 2, in); /* maximum sprites in this file */
  120.    fwrite(&maxsprite, 1, 2, out); 
  121.    for (i = startingsprite; i <= maxsprite; i++) /* load them in */
  122.      {
  123.       fread(&spritemade, 1, 2, in);    /* flag to see if sprite exists */
  124.       fwrite(&spritemade, 1, 2, out); 
  125.       if (spritemade == 1)
  126.         {
  127.          fg_setcolor (0);
  128.      fg_rect (0, fg_getmaxx (), 0, fg_getmaxy ());
  129.          /* maximum sprite size */
  130.  
  131.          fread(&a, 1, 2, in); /* get width and height */
  132.          fread(&b, 1, 2, in);
  133.          fwrite(&a, 1, 2, out); /* put width and height */
  134.          fwrite(&b, 1, 2, out); 
  135.  
  136.      /* Read in the image data. Each byte represents a color
  137.         from 0-255. Obviously converting sprites that use more colors
  138.         than the current mode allows will not work. Draw sprites using
  139.         only the first colors (eg 0-16), up to maxcolors of the mode 
  140.             you're using.  */
  141.          for (y = 0; y < b; y++)
  142.        for (x = 0; x < a; x++)
  143.         {
  144.          c = fgetc (in);
  145.          fg_setcolor (c);
  146.          fg_point (x, y);
  147.         }
  148.  
  149.      size = fg_imagesiz(a - 1, b - 1); /* get byte size of image */
  150.      if ((temp = (char far *)farmalloc (size)) == NULL)
  151.        {
  152.         fg_setmode (oldmode);
  153.         printf ("Error: not enough heap space in convertsprites.\n");
  154.         exit (1);
  155.        }
  156.      fg_move (0, b);
  157.      fg_getimage(temp, a - 1, b - 1); /* Get the image in new mode */
  158.          fwrite(temp, size, 1, out); /* Write the data in getimage format */
  159.      farfree (temp);
  160.     }
  161.      }
  162.   }
  163.  fclose (in);
  164.  fclose (out);
  165. }
  166.  
  167.  
  168. void loadsprites (char *infile, void far *loadspr[])
  169. {
  170. FILE *in;   /* converted color sprite file */
  171. int maxsprite;
  172. unsigned size;
  173.  
  174. int a, b, i, spritemade;
  175. int maxcolor;
  176. struct {
  177.         unsigned char red, green, blue;
  178.        } pal[256];
  179.  
  180.     /* Open the files */
  181.  if ((in = fopen (infile, "rb")) == NULL)
  182.    {
  183.     fg_setmode (oldmode);
  184.     printf ("Could not load converted color sprite file");
  185.     exit (1);
  186.    }
  187.  
  188.  fread(&maxcolor, 1, 2, in);
  189.  
  190.  fg_getdacs (0, 255, (char far *)pal);
  191.  for (i = 0; i < maxcolor; i++) /* read in the palette */
  192.    {
  193.     pal[i].red = fgetc (in);
  194.     pal[i].green = fgetc (in);
  195.     pal[i].blue = fgetc (in);
  196.    }
  197.  fg_setdacs (0, 255, (char far *)pal);
  198.  
  199.  fread(&maxsprite, 1, 2, in); /* maximum sprites in this file */
  200.  
  201.  for (i = 0; i < maxsprite; i++) /* load them in */
  202.    {
  203.     fread(&spritemade, 1, 2, in); /* flag to see if sprite exists */
  204.     if (spritemade == 1)
  205.       {
  206.        fread(&a, 1, 2, in);  /* get width and height */
  207.        fread(&b, 1, 2, in);
  208.        width[i] = a - 1;
  209.        height[i] = b - 1;
  210.  
  211.        size = fg_imagesiz(a - 1, b - 1); /* get byte size of image */
  212.        if ((loadspr[i] = farmalloc (size)) == NULL)
  213.          {
  214.           fg_setmode (oldmode);
  215.       printf ("Error: not enough heap space in loadsprites().\n");
  216.       freesprites (loadspr);
  217.       exit (1);
  218.      }
  219.        fread (loadspr[i], size, 1, in);
  220.       } 
  221.       else loadspr[i] = NULL;
  222.      }
  223.     fclose (in);
  224. }
  225.  
  226.  
  227.  
  228.  
  229. void testsprites(void)
  230. /* Loops through 10 sprites, displaying them on the screen
  231.    using the putimage method. Press a key to go to the next sprite */
  232. {
  233. int i, j;
  234.  
  235.  for (i = 0; i < 10; i++) 
  236.   {
  237.    fg_erase ();
  238.  
  239.    if (sprites[i] != NULL)
  240.      {
  241.       for (j = 1; j < 20; j++)
  242.        {
  243.         fg_move (rand () % fg_getmaxx (),rand () % fg_getmaxy ());
  244.         fg_drwimage ((char far *)sprites[i], width[i], height[i]);
  245.        }
  246.        /* Put the converted image on the screen */
  247.       getch ();
  248.      }
  249.   }
  250. }
  251.  
  252.  
  253. void freesprites(void far *freespr[])
  254. {
  255. int i;
  256.  
  257.  for (i = 0; i < 2001; i++)
  258.    {
  259.     if (freespr[i] != NULL)
  260.       farfree (freespr[i]);
  261.    }
  262. }